package com.wbl.service.impl;

import com.wbl.domain.StudentCourse;
import com.wbl.service.OuterDataQueryService;
import com.wbl.dao.OuterDataDao;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by hrd on 2016/8/8.
 */
@Service(value = "outerDataQueryService")
public class OuterDataQueryServiceImpl implements OuterDataQueryService {
        private static Logger logger = Logger.getLogger(OuterDataQueryServiceImpl.class);
        @Autowired
        private OuterDataDao outerDataDao;


    @Override
    public List<JSONObject> QueryCouseById(String stuId) {
        System.out.println(outerDataDao.getUserInfo("4") + "----------------------------------------");
        System.out.println(outerDataDao.getCourseById2("----------------->" + "08212830"));
        List<StudentCourse> courses = outerDataDao.getCourseById(stuId);
        List<JSONObject> results = new ArrayList<>();
        JSONObject baseMessage = new JSONObject();
        if (courses == null){
            logger.debug("there is not the student's course message");
            baseMessage.put("查询ID", stuId);
            baseMessage.put("status", "none");
            results.add(baseMessage);
        }else {
            int length=courses.size();
            logger.debug("there are " + length + " the student's course messages");
            baseMessage.put("查询ID", stuId);
            baseMessage.put("status", "success");
            results.add(baseMessage);
            StudentCourse course;
            for(int i=0;i<length;i++){
                JSONObject object=new JSONObject();
                course=courses.get(i);
                object.put("编号",i+1);
                object.put("课程ID",course.getCourseId());
                object.put("课程名字",course.getCourseName());
                object.put("任课教师",course.getTeacherName());
                results.add(object);
            }
        }
         return results;
    }

    @Override
    public String myTest(String id) {
        System.out.println(outerDataDao.getUserInfo(id));
        return "";
    }

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        OuterDataQueryService outerDataQueryService = context.getBean("outerDataQueryService",OuterDataQueryService.class);
        outerDataQueryService.myTest("4");
    }

}
